package com.itheima.threadpool;

import java.util.concurrent.*;

/*
自己使用构造方法  创建线程池对象
 */
public class Demo03 {

    public static void main(String[] args) {
        //创建阻塞队列 用来 排队的
        ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(4);
        //创建线程工厂 用来 生产线程的
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        //拒绝策略对象   ThreadPoolExecutor 的静态内部类
        // 拒绝并报错  抛出异常
        ThreadPoolExecutor.AbortPolicy abortPolicy = new ThreadPoolExecutor.AbortPolicy();
        //拒绝 啥也不干 活没干 没抛异常  把最后的任务丢弃了
        ThreadPoolExecutor.DiscardPolicy discardPolicy = new ThreadPoolExecutor.DiscardPolicy();
        //把等的最久的拒绝了  活没干  没抛异常  把 等的最久的任务丢弃了
        ThreadPoolExecutor.DiscardOldestPolicy discardOldestPolicy = new ThreadPoolExecutor.DiscardOldestPolicy();
        // 直接干活     没有使用线程池中的线程 执行任务  而是 使用main线程干活了   thread.run
        ThreadPoolExecutor.CallerRunsPolicy callerRunsPolicy = new ThreadPoolExecutor.CallerRunsPolicy();


        ThreadPoolExecutor pool = new ThreadPoolExecutor(3/*核心线程数*/,
                8/*最大线程数=核心+临时*/,
                2/*等待多久被开*/,
                TimeUnit.DAYS/*等待多久被开的单位*/,
                queue,
                threadFactory,
                callerRunsPolicy
                );
        for (int i = 0; i < 13; i++) {
            int j=i;
            pool.submit(()->{
                String name = Thread.currentThread().getName();
                System.out.println(name+"正在执行任务了"+ j);
            });
        }

        pool.shutdown();
    }

}
